package mark.tom.detector;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import java.io.File;

import mark.tom.detector.template.Detector;
import mark.tom.detector.template.Logger;

import static mark.tom.detector.Util.checkNull;
import static mark.tom.detector.Util.getLogger;

/**
 * @Description: 模拟器检测器
 * @Author: ttmk110@163.com
 * @Package: mark.tom.detector
 * @Date: 2020/3/21
 * @Copyright: 版权归 Mark 所有
 * <ModifyLog>
 * @ModifyContent:
 * @Author:
 * @Date: </ModifyLog>
 */
public class SimulatorDetector extends Detector {

    private com.alibaba.wireless.security.jaq.SimulatorDetect simulatorDetect;
    private Logger logger;

    public SimulatorDetector(@NonNull Context context) {
        this(context, false);
    }

    public SimulatorDetector(@NonNull Context context, boolean log) {
        checkNull("context must not be null", context);
        simulatorDetect = new com.alibaba.wireless.security.jaq.SimulatorDetect(context);
        logger = getLogger(log);
    }

    @Override
    public boolean check() {
        boolean result = false;
        try {
            result = checkWhEx();
        } catch (Exception ex) {
            logger.log(ex.getMessage(), Log.ERROR);
        }
        return result;
    }

    @Override
    public boolean checkWhEx() throws Exception {
        boolean isSimulator = simulatorDetect.isSimulator();
        if (!isSimulator) {
            // 判断是否是BlueStacks内核
            isSimulator = isBlueStacks();
        }
        if (!isSimulator) {
            // 判断是否是Mumu内核
            isSimulator = isMuuKernel();
        }
        return isSimulator;
    }

    /**
     * @Description: 判断是不是blueStack内核的模拟器
     * @Author: shentao@hsyuntai.com
     * @Date: 2016/12/9
     */
    @SuppressLint("SdCardPath")
    private boolean isBlueStacks() {
        String[] known_bluestacks = {"/data/app/com.bluestacks.appmart-1.apk", "/data/app/com.bluestacks.BstCommandProcessor-1.apk",
                "/data/app/com.bluestacks.help-1.apk", "/data/app/com.bluestacks.home-1.apk", "/data/app/com.bluestacks.s2p-1.apk",
                "/data/app/com.bluestacks.searchapp-1.apk", "/data/bluestacks.prop", "/data/data/com.androVM.vmconfig",
                "/data/data/com.bluestacks.accelerometerui", "/data/data/com.bluestacks.appfinder", "/data/data/com.bluestacks.appmart",
                "/data/data/com.bluestacks.appsettings", "/data/data/com.bluestacks.BstCommandProcessor", "/data/data/com.bluestacks.bstfolder",
                "/data/data/com.bluestacks.help", "/data/data/com.bluestacks.home", "/data/data/com.bluestacks.s2p", "/data/data/com.bluestacks.searchapp",
                "/data/data/com.bluestacks.settings", "/data/data/com.bluestacks.setup", "/data/data/com.bluestacks.spotlight", "/mnt/prebundledapps/bluestacks.prop.orig"
        };
        for (String fileName : known_bluestacks) {
            File qemuFile = new File(fileName);
            if (qemuFile.exists()) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description: 判断是不是Mumu内核的模拟器
     * @Author: shentao@hsyuntai.com
     * @Date: 17/7/13
     */
    private boolean isMuuKernel() {
        String[] known_muu = {
                "/system/app/nemu-android-launcher-pack.apk",
                "/system/bin/nemuVM-nemu-service",
                "/system/bin/nemuVM-nemu-sf",
                "/system/bin/nemuVM-prop"
        };
        for (String fileName : known_muu) {
            File muuFile = new File(fileName);
            if (muuFile.exists()) {
                return true;
            }
        }
        return false;
    }

}
